import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { getSearchList } from '@/pages/ContractController/TemplateManage/mconfig';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { contractManageSwitch } from '../mconfig';

const baseUrl = getBaseUrl();
const templateManageService = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}`, //范本状态
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_TYPE}`, //范本类别
    `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_CANCELTYPE}`, //作废类型
  ];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = contractManageSwitch(curRes);
  // const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  /** 获取搜索参数 */
  const searchListParams = getSearchList(loadMdicMapObj?.loadMapInfo, queryTypeObj);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 5,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };

      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        dicTransName: 'state,businessType,templateType,cancelType',
        orgTransName: `${ISearchTranslateAll.SIGN_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });
      searchParams.whereParams = [
        {
          fieldName: queryTypeObj?.params,
          fieldType: 'String',
          fieldParam:
            queryTypeObj?.params == 'ownDepartmentID' ? '5' : queryTypeObj?.params == 'ownDepartmentID' ? '4' : queryTypeObj?.params == 'ownerID' ? '2' : '3',
          fieldUse: 0,
          searchType: 2,
          seq: 0,
          valueInfo: searchParams?.whereParams[0]?.valueInfo,
        },
      ];
      searchParams.whereParams.push({
        fieldName: 'state',
        fieldType: 'String',
        fieldParam: 1,
        fieldUse: 0,
        isSearch: 1,
        searchType: 1,
        seq: 0,
        valueInfo: '100123021002',
      });
      if (opt?.fullName) {
        searchParams.whereParams.push({
          fieldName: 'fullName',
          fieldType: 'String',
          fieldParam: 0,
          fieldUse: 0,
          isSearch: 1,
          searchType: 0,
          seq: 0,
          valueInfo: opt?.fullName,
        });
      }
      if (opt?.templateInfo) {
        searchParams.whereParams.push({
          fieldName: 'templateInfo',
          fieldType: 'String',
          fieldParam: 0,
          fieldUse: 0,
          isSearch: 1,
          searchType: 0,
          seq: 0,
          valueInfo: opt?.templateInfo,
        });
      }
      // searchParams.whereParams.push(opt?.whereParams[0])
      const requestURL = `${baseUrl}/purchaseSourceController/dataContractTemplateGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, searchParams);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 5,
    },
  );

  /* 获取页面数据 */
  const getTempContractData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    if (request?.singTime) {
      const [singTimeStart, singTimeEnd] = request.singTime;
      request['singTimeStart'] = singTimeStart.format('YYYY-MM-DD 00:00:00');
      request['singTimeEnd'] = singTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['singTime'];
    }

    if (request?.cancelTime) {
      const [cancelTimeStart, cancelTimeEnd] = request.cancelTime;
      request['cancelTimeStart'] = cancelTimeStart.format('YYYY-MM-DD 00:00:00');
      request['cancelTimeEnd'] = cancelTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['cancelTime'];
    }

    delete request['signFormNameDept'];
    delete request['signFormNameUser'];
    if (request?.signerID && request?.signerID?.length > 0) {
      request['signerID'] = request?.signerID?.toString();
    } else {
      delete request['signerID'];
    }
    run({
      ...request,
    });
  };

  /** 搜索列表  */
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getTempContractData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};
export { templateManageService };
